package org.yty.travelagencymongodb.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.yty.travelagencymongodb.model.Entity.Result;
import org.yty.travelagencymongodb.model.Entity.TourGroup;
import org.yty.travelagencymongodb.model.Entity.TourGroupTraveller;
import org.yty.travelagencymongodb.model.Entity.Traveller;
import org.yty.travelagencymongodb.service.TourGroupTravellerService;

import java.util.List;

@RestController
@RequestMapping("tour-group-traveller")
@RequiredArgsConstructor
public class TourGroupTravellerController {
    private final TourGroupTravellerService tourGroupTravellerService;

    @GetMapping("/getTourGroupListByTravellerId/{travellerId}")
    public Result<List<TourGroup>> getTourGroupListByTravellerId(@PathVariable Long travellerId) {
        List<TourGroup> list = this.tourGroupTravellerService.getTourGroupListByTravellerId(travellerId);
        if (list == null || list.size() == 0){
            return Result.error("暂无数据");
        }
        return Result.ok(list);
    }

    @GetMapping("/getTravellerListByTourGroupId/{tourGroupId}")
    public Result<List<Traveller>> getTravellerListByTourGroupId(@PathVariable Long tourGroupId) {
        List<Traveller> list = this.tourGroupTravellerService.getTravellerListByTourGroupId(tourGroupId);
        if (list == null || list.size() == 0){
            return Result.error("暂无数据");
        }
        return Result.ok(list);
    }

    @DeleteMapping("/deleteByTourGroupId/{tourGroupId}")
    public Result<Void> deleteByTourGroupId(@PathVariable Long tourGroupId) {
        this.tourGroupTravellerService.deleteByTourGroupId(tourGroupId);
        return Result.ok();
    }

    @DeleteMapping("/deleteByTravellerId/{travellerId}")
    public Result<Void> deleteByTravellerId(@PathVariable Long travellerId) {
        this.tourGroupTravellerService.deleteByTravellerId(travellerId);
        return Result.ok();
    }

    @DeleteMapping("/{tourGroupId}/{travellerId}")
    public Result<Void> deleteByTourGroupIdAndTravellerId(@PathVariable Long tourGroupId, @PathVariable Long travellerId) {
        this.tourGroupTravellerService.deleteByTourGroupIdAndTravellerId(tourGroupId, travellerId);
        return Result.ok();
    }

    @PostMapping
    public Result<Void> save(@RequestBody TourGroupTraveller tourGroupTraveller) {
        this.tourGroupTravellerService.save(tourGroupTraveller);
        return Result.ok();
    }
}
